﻿using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Acme.BookStore.Navigates
{
    public class NavigateAppService : BookStoreAppService, INavigateAppService
    {
        private readonly SqlSugarScope db; // 核心对象：拥有完整的SqlSugar全部功能 单例

        public NavigateAppService(SqlSugarScope Db)
        {
            db = Db;
        }

        [IgnoreAntiforgeryToken]
        public async Task<List<StudentDto>> GetListAsync(string filter)
        {
            var list = await db.Queryable<Student>()
              .Includes(x => x.School, x => x.RoomList)//2个参数就是 查询2级 ，最多支持 7级
              .Includes(x => x.Books)
              .WhereIF(!filter.IsNullOrWhiteSpace(), x => x.School.SchoolName.Contains(filter))
              .ToListAsync();
            //手动映射
            db.ThenMapper(list, stu =>
            {
                //如果加Where不能带有stu参数，stu参数写到 SetContext. SetContext不会生成循环操作，高性能  和直接Where性能是不一样的
                stu.School.TeacherList = db.Queryable<Teacher>().SetContext(teacher => teacher.SchoolId, () => stu.SchoolId, stu);
            });
            return ObjectMapper.Map<List<Student>, List<StudentDto>>(list);
        }
    }
}